<?php
/**
 * 
 * @package avp
 * @version $Id: $
 * @copyright (c) 2006 Jan-Willem van Ganswijk, Stefan van Essen
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
if(!defined('IN_SCRIPT')) {
    die('Baaaaad hax0r');
} 

function check_hotlink($id, $referer_str, $mode, $trusted_domains) {
    if($mode == 'none' || empty($trusted_domains) || empty($referer_str))
        return true;

    global $db;
    $allowed = false;

    $trusteddomains = explode(';', $trusted_domains);
    $url = explode('//', $referer_str); // sloop http:// weg
    $url = explode('/', $url[1]); // de rest in stukken choppen, en het eerste stuk is dan je domain.
    foreach($trusteddomains as $checkdomain) {
        if(stristr($url[0], $checkdomain)) {
            $allowed = true;
        } 
    } 
    if($allowed == true) {
        return true;
    } elseif($mode == 'active') {
        $db->query("UPDATE `" . $db->get_table("avatar") . "` SET `hc_blockcount`=`hc_blockcount`+1 WHERE `id`='{$id}'");
        return false;
    } elseif($mode == 'passive') {
        $result = $db->query("SELECT * FROM `" . $db->get_table("hotlinkcheck") . "` WHERE `hc_domain`='{$url[0]}' AND `avatarid`='{$id}'");
        $results = $db->get_num_rows($result);

        if($results < 1) {
            $db->query("INSERT INTO `" . $db->get_table("hotlinkcheck") . "` (`hc_domain`,`hc_full_referer`,`avatarid`) VALUES ('{$url[0]}','{$referer_str}','{$id}')");
            return true;
        } 
        $row = $db->fetch($result);
        $db->query("UPDATE `" . $db->get_table("hotlinkcheck") . "` SET `hc_count`=`hc_count`+1 WHERE `hc_id`='" . $row['hc_id'] . "' AND  `avatarid`='{$id}'");
        if($row['hc_allow'] == 1) {
            return true;
        } else {
            $db->query("UPDATE `" . $db->get_table("avatar") . "` SET `hc_blockcount`=`hc_blockcount`+1 WHERE `id`='{$id}'");
            return false;
        } 
    } 
} 

?>